//
//  RNOLHomePageViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/3/30.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLHomePageViewController.h"
#import "RNOLHomeRequest.h"
#import "RNOLHomePageView.h"
#import "RNOLNewHandInvestViewController.h"
#import "RNOLHomeNavgationBar.h"
#import "YYFPSLabel.h"

#import "RNOLClaimTransferViewController.h"


@interface RNOLHomePageViewController ()<TBActionSheetDelegate,UIScrollViewDelegate,RNOLHomePageViewProtocol,UINavigationControllerDelegate>

@property (nonatomic,strong) RNOLHomePageView *homePageView;
@property (nonatomic,strong)  UIScrollView *bgScrollView ;
@property (nonatomic,strong) UILabel *titleLabel ;
@property (nonatomic,strong) RNOLHomeNavgationBar *homeNavgationBar;
@end


@implementation RNOLHomePageViewController

- (UIScrollView *)bgScrollView{
    if (!_bgScrollView) {
        _bgScrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, KWIDTH , KHIGHT - kTabBarHeight )];
        adjustsScrollViewInsets_NO(_bgScrollView, self);
        _bgScrollView.backgroundColor = kBgColor ;
        _bgScrollView.delegate = self;
        [_bgScrollView addHeaderRefreshWithTarget:self eventAction:@selector(requestHomePageData)];
        [_bgScrollView addSubview: self.homePageView];
        _bgScrollView.contentSize = CGSizeMake(KWIDTH, self.homePageView.height);
        [self.view addSubview: _bgScrollView];
    }
    return _bgScrollView;
}

- (RNOLHomeNavgationBar *)homeNavgationBar{
	if (!_homeNavgationBar) {
		_homeNavgationBar = [[RNOLHomeNavgationBar alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , kNavBarHeight)];
		[self.view addSubview:_homeNavgationBar];
		[self.view insertSubview:_homeNavgationBar aboveSubview:self.bgScrollView];
		[_homeNavgationBar setClearBar];
	}
	return _homeNavgationBar;
}

- (RNOLHomePageView *)homePageView{
    if (!_homePageView) {
        _homePageView = [[RNOLHomePageView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT)];
        _homePageView.height = [_homePageView homepageMaxHeight];
        _homePageView.delegate = self;
        _homePageView.backgroundColor = kBgColor ;
    }
    return _homePageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self requestHomePageData];
    
    [[kNotificationCenter rac_addObserverForName:kNetworkingReachabilityStateChangeNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        BOOL haveNetwork = [x.object integerValue] >= 1;
        self.homePageView.hidden = !haveNetwork;
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      [self.navigationController setNavigationBarHidden:YES animated:[RNOLLoginTool neddNavgationBarAnimation]];
     [RNOLLoginTool setNavgationBarNeedAnimation:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [self.navigationController setNavigationBarHidden:NO animated:[RNOLLoginTool neddNavgationBarAnimation]];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)setupUI{
    self.view.backgroundColor =  kBgColor;
    [self bgScrollView];
    [self homePageView];
    [self homeNavgationBar];
    
#ifdef DEBUG
    [self addDebugView];
#else
    NSLog(@"6666");
#endif
}


#pragma mark- 请求数据
- (void)requestHomePageData{
        [self.bgScrollView releaseRefresh];
        RNOLHomeRequest * request =  [[RNOLHomeRequest alloc] init];
        id params = [request yy_modelToJSONObject];
    
        [request PostRequestWithParameters: params callBack:^(id resultData, NSError *error) {
            NSLog(@"首页数据----- \n\t %@",resultData);
        }];
    
}


#pragma mark- 调试工具
- (void)addDebugView{
    
    
    WMDragView *debugView = [[WMDragView alloc] initWithFrame:CGRectMake(KWIDTH - 80 , KHIGHT - kTabBarHeight - 280 , 70, 70)];
    ViewBorderRadius(debugView, 35, 2, KWhiteColor);
    debugView.isKeepBounds = YES;
    debugView.button.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [debugView.button setTitleColor:KWhiteColor forState:UIControlStateNormal];
    YYFPSLabel *fpsLabel = [YYFPSLabel new];
    fpsLabel.textColor = KWhiteColor;
    fpsLabel.frame = CGRectMake(10, 20, 70 , 20);
    [fpsLabel sizeToFit];
    fpsLabel.userInteractionEnabled = NO;
    [debugView addSubview: fpsLabel];
    
    debugView.backgroundColor = [UIColor blackColor];
    [debugView setLayerShadow: KOrangeColor  offset: CGSizeMake(-1, 1)  radius: 5];
    [kWindow addSubview: debugView];
    debugView.clickDragViewBlock = ^(WMDragView *dragView){
        NSString *title = kWGB_STR(@"当前是%@ - %@",[RNOLServerHostManager currentServerHostEnvironmentName],[RNOLServerHostManager currentServerHost]);
        TBActionSheet *sheet = [[TBActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:@"线上环境" otherButtonTitles:@"测试环境",@"dev环境", nil];
        [sheet show];
    };
    
    // 长按隐藏
    @weakify(debugView);
    [debugView addLongPressActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(debugView);
        [self wgbStyle_alertWithTitle:@"你确定要隐藏这个Debug悬浮窗吗?" cancelButtonTitle:@"考虑一下" confirmButtonTitle:@"确定" cancelAction:nil comfirmAction:^{
            debugView.hidden = YES;
        }];
    }];
}

- (void)actionSheet:(TBActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 3) {//取消
        return ;
    }
    // 0. 线上 1.测试 2.dev
    RNOLServerHostEnvironmentType serverType = (RNOLServerHostEnvironmentType)buttonIndex;
    [RNOLServerHostManager setAPIServerHostType: serverType];
    exit(0); //退出程序
}


#pragma mark- <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > kNavBarHeight) {
        if (offsetY > kNavBarHeight*2) {
            offsetY = kNavBarHeight*2;
        }
        CGFloat alpha = (offsetY - kNavBarHeight)/kNavBarHeight;
				self.homeNavgationBar.alpha = alpha; ;
        self.homeNavgationBar.titleLabel.alpha = alpha;
    }else{
			[self.homeNavgationBar setClearBar];
		}
}

#pragma mark - <RNOLHomePageViewProtocol>
- (void)bannerClickEventWithModel:(id)model atInex:(NSInteger)index {

//    RNOLClaimTransferViewController *claimVC = [[RNOLClaimTransferViewController alloc] init];
//    [self.navigationController pushViewController:claimVC animated:YES];
//    [self showStatusTipWithText:@"banner"];


}

- (void)indexIconViewClickEventWithModel:(id)model atInex:(NSInteger)index {
    [self showLoginViewController];
    
//    [self showStatusTipWithText: @(index+1).stringValue];
}

//新手标
- (void)noobSpecialViewClickEventAction {
    RNOLNewHandInvestViewController *newHandVC = [[RNOLNewHandInvestViewController alloc] init];
    [self.navigationController pushViewController:newHandVC animated:YES];
}

- (void)noticeClickEventWithModel:(id)model atInex:(NSInteger)index {
    [self showStatusTipWithText: @(index+1).stringValue];
}


- (void)noticeClickMoreEventAction {
    [self showStatusTipWithText:@"更多"];

}


@end
